package com.wanmao.test.service;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.wanmao.test.bean.XMBean;
import com.wanmao.test.dbhelper.DBOpenHelper;

import java.util.ArrayList;
import java.util.List;


public class XMService {
    private DBOpenHelper dBOpenHelper;

    public XMService(Context context) {
        this.dBOpenHelper = new DBOpenHelper(context);
    }

    public void save(XMBean bean) {
        SQLiteDatabase db = dBOpenHelper.getWritableDatabase();
        db.insert("XM_TABLE", null, settingValue(bean));
    }


    private ContentValues settingValue(XMBean bean) {
        ContentValues values = new ContentValues();
        values.put("XM_ID",bean.getXM_ID());
        values.put("XZID",bean.getXZID());
        values.put("XMMC",bean.getXMMC());
        values.put("XMQZ",bean.getQZ());
        values.put("JDYQ",bean.getJDYJ());
        values.put("JDJD",bean.getJDJD());
        return values;
    }


    public List<XMBean> findXZID(String XZid){
        String sql = "select * from XM_TABLE where XZID=?";
        SQLiteDatabase db = dBOpenHelper.getReadableDatabase();
        Cursor cursor = db.rawQuery(sql,new String[]{XZid});
        return toListBean(cursor);
    }
    public List<XMBean> findXZID(String XZid,String JDJD){
        String sql = "select * from XM_TABLE where XZID=?  and JDJD=?";
        SQLiteDatabase db = dBOpenHelper.getReadableDatabase();
        Cursor cursor = db.rawQuery(sql,new String[]{XZid,JDJD});

        return toListBean(cursor);

    }
    private List<XMBean> toListBean(Cursor cursor){
        List<XMBean> list=new ArrayList<>();
        while (cursor.moveToNext()){
            XMBean bean=new XMBean();
            bean.setXZID(cursor.getString(cursor.getColumnIndex("XZID")));
            bean.setXM_ID(cursor.getString(cursor.getColumnIndex("XM_ID")));
            bean.setXMMC(cursor.getString(cursor.getColumnIndex("XMMC")));
            bean.setJDYJ(cursor.getString(cursor.getColumnIndex("JDYQ")));
            bean.setQZ(cursor.getString(cursor.getColumnIndex("XMQZ")));
            bean.setJDJD(cursor.getString(cursor.getColumnIndex("JDJD")));
            list.add(bean);
        }
        return list;
    }
}
